package action;

import java.text.ParseException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import domain.ScheduledJob;

@Controller
public class ScheduleAction {

	private Log LOGGER = LogFactory.getLog(this.getClass());

	private SchedulerFactory schedulerFactory;

	private SchedulerFactoryBean schedulerFactoryBean;

	@RequestMapping("schedule")
	public ModelAndView execute() throws SchedulerException, ParseException {
		LOGGER.info("Schedule");

		Scheduler scheduler = schedulerFactory.getScheduler();
		Scheduler scheduler1 = schedulerFactoryBean.getScheduler();



		scheduler.start();
		
		
		JobDetail jobDetail = new JobDetail("scheduleJob", "grupa1", ScheduledJob.class);

		CronTrigger trigger = new CronTrigger("cronTrigger", "grupa1", "0/10 * 17 * * ?");
		scheduler.scheduleJob(jobDetail, trigger);

		ModelAndView mav = new ModelAndView("schedule");
		return mav;
	}

	@Autowired
	public void setSchedulerFactory(SchedulerFactory schedulerFactory) {
		this.schedulerFactory = schedulerFactory;
	}

	@Autowired
	public void setSchedulerFactoryBean(SchedulerFactoryBean schedulerFactoryBean) {
		this.schedulerFactoryBean = schedulerFactoryBean;
	}

}
